package dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import user.Manager;
import user.Student;
import util.DbUtil;
import util.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ManagerDaoImp implements ManagerDao{

    @Override
    public Manager managerLogin(Manager manager) {
        Connection con = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1、获取数据库连接
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            //2、准备sql语句
            String sql = "select * from homework.manager where account = ? and password = ?";
            //3、执行后获取结果并返回
            Object[] params ={manager.getAccount(),manager.getPassword()};
            Manager manager1 = qr.query(sql,new BeanHandler<Manager>(Manager.class),params);
            return manager1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                DruidUtils.close(con, preparedStatement);
                if(null != resultSet){
                    resultSet.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
       return null;
    }
}
